use ggez::{glam::Vec2, Context, graphics};

use crate::{balloon::Balloon, mover::Mover};



pub struct Spring {
    anchor: Vec2,
    len: f32,
    k: f32,
    location: Vec2
}

impl Spring {
    pub fn new(anchor: Vec2, len: f32) -> Spring {
        let k = 0.00001;
        let location = Vec2{x: 0.0, y: 0.0};
        Spring { anchor, len, k, location}
    }

    pub fn connect(&mut self, bob: &mut Balloon) -> () {
        let mut force = bob.get_location() - self.anchor;
        let d = force.length();
        let stretch = d - self.len;
        force *= -1.0*self.k*stretch;
        bob.apply_force(force);
        self.location = bob.get_location();
    }

    pub fn update(&mut self, _ctx: &mut Context) -> () {
    }

    pub fn display(&self, ctx: &mut Context, canvas : &mut graphics::Canvas) -> () {
        let line = graphics::Mesh::new_line(
            ctx,
            &[self.anchor, self.location],
            2.0,
            graphics::Color::BLACK,
        ).unwrap();
        canvas.draw(&line, graphics::DrawParam::default());
    }

}